/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-02-05
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.CouponCodeService;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.basics.entity.CouponCodeGift;
import com.rzico.basics.service.CouponCodeGiftService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 提货券转账记录控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2021-02-05
 */
@Api(description = "提货券转账记录接口")
@RestController
@RequestMapping("/admin/couponCodeGift")
public class CouponCodeGiftAdminController extends BaseController {

    @Autowired
    private CouponCodeGiftService couponCodeGiftService;

    @Autowired
    private CouponCodeService couponCodeService;

    @Autowired
    private MemberService memberService;

    /**
     * 分页查询提货券转账记录
     *
     * @return
    */
    @ApiOperation("分页查询提货券转账记录")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query")
    })
    public CommResult<CouponCodeGift> list(Date startDate, Date endDate, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CouponCodeGift> list = couponCodeGiftService.selectList(params);
        PageResult<CouponCodeGift> pageResult = new PageResult<CouponCodeGift>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条提货券转账记录
     *
     * @return
    */
    @ApiOperation("查询单条提货券转账记录")
    @GetMapping("/find/{id}")
    public CommResult<CouponCodeGift> find(@PathVariable String id) {

        CouponCodeGift result = couponCodeGiftService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("couponCodeGift",result);
        return CommResult.success(data);

    }

    /**
     * 保存提货券转账记录
     *
     * @param couponCodeGift
     * @return
     */
    @ApiOperation("保存提货券转账记录")
    @PostMapping("/save")
    public CommResult<CouponCodeGift> save(@RequestBody List<CouponCodeGift> couponCodeGift) {
        Member member = memberService.getCurrent();
        for (CouponCodeGift gift:couponCodeGift) {
            gift.setMemberId(member.getId());
            gift.setStatus(0);
            gift.setEnterpriseId(member.getEnterpriseId());
        }

        try {
            couponCodeGiftService.save(couponCodeGift);
        } catch (Exception e) {
            return CommResult.error(e.getMessage());
        }

        return CommResult.success();

    }


    /**
     * 批量删除提货券转账记录
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除提货券转账记录", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除提货券转账记录,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<CouponCodeGift> del(@PathVariable String ids) {

        couponCodeGiftService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 更新提货券转账记录
     *
     * @param couponCodeGift
     * @return
     */
    @Log(desc = "更新提货券转账记录", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新提货券转账记录")
    @PostMapping("/update")
    public CommResult<CouponCodeGift> update(@RequestBody CouponCodeGift couponCodeGift) {
        int affectCount = couponCodeGiftService.updateByPrimaryKeySelective(couponCodeGift);
        if (affectCount <= 0){
            return CommResult.error();
        }
        CouponCodeGift result = couponCodeGiftService.findById(couponCodeGift.getId());
        return CommResult.success(result);
    }

}
